package handler

import (
	"github.com/gin-gonic/gin"
	"live-broadcast/api/request"
	"live-broadcast/internal/dao"
	"live-broadcast/internal/model"
	"net/http"
)

func AddLexicon(c *gin.Context) {
	var req request.AddLexiconForm
	var lexicon model.Lexicon

	if err := c.ShouldBind(&req); err != nil {
		return
	}

	if req.Text == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	err := dao.GetOneFile(&model.Lexicon{Text: req.Text}, &lexicon)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "添加失败",
			"data": false,
		})
		return
	}

	if lexicon.Id > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "添加失败,违禁词已存在",
			"data": false,
		})
		return
	}
	lexicon.Text = req.Text

	err = dao.AddFile(&lexicon)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "添加失败",
			"data": false,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加成功",
		"data": lexicon.Id,
	})

}

func DeleteLexicon(c *gin.Context) {
	var req request.DeleteLexiconForm
	var lexicon model.Lexicon

	if err := c.ShouldBind(&req); err != nil {
		return
	}

	if req.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	lexicon.Id = req.Id
	err := dao.DeleteFile(&lexicon)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "删除成功",
		"data": true,
	})

}

func FindLexicon(c *gin.Context) {

}
